﻿#include "GY_ControlTreeView.h"
#include "RegisterControlBase.h"

GY_ControlTreeView::GY_ControlTreeView(QWidget *parent)
    : GY_IdTreeView(parent)
{

}

GY_ControlTreeView::~GY_ControlTreeView()
{

}

/**
 * @brief addRowData  添加行数据
 * @param rowId 行Id
 * @param rowData 行数据
 * @param nodeParentId 父级Id
 * @param nodeType 节点类型
 * @return 返回添加是否成功
 */
bool GY_ControlTreeView::addRowData(const QVariant &rowId, const QVector<QVariant> &rowData, const QVariant &nodeParentId, const QVariant &nodeType)
{
    //添加一行数据
    if(!GY_IdTreeView::addRowData(rowId,rowData,nodeParentId,nodeType)){
        return false;
    }
    //循环这一行那些列需要插入控件
    QMap<qint32,QString>::iterator iter;
    for(iter = m_RegisterInfo.begin(); iter!=m_RegisterInfo.end(); ++iter)
    {
        QWidget* pWidget=(QWidget*)m_Reflection.newInstance(iter.value().toLatin1(),Q_ARG(QWidget*,this));
        if(pWidget==nullptr)
        {
            continue;
        }
        //查看对象是否继承自RegisterControlBase类
        if(!pWidget->inherits("RegisterControlBase"))
        {
            pWidget->deleteLater();
            continue;
        }
        //添加控件
        setIndexWidget(m_pFilter->mapFromSource(m_TreeNodeInfo[rowId][iter.key()]->index()),pWidget);
        //让控件显示应有的值
        ((RegisterControlBase*)pWidget)->setData(rowData[iter.key()]);
        ((RegisterControlBase*)pWidget)->setPosition(rowId,iter.key());
        //清空需要插入控件的单元格内容,锁住信号，避免修改displayData的时候触发信号导致transferData一并修改
        m_pModel->blockSignals(true);
        m_TreeNodeInfo[rowId][iter.key()]->setText("");
        m_pModel->blockSignals(false);
        //绑定控件点击事件和表格的点击信号
        connect(((RegisterControlBase*)pWidget), &RegisterControlBase::controlDataUpdate, this, &GY_ControlTreeView::slt_ControlDataUpdate);
    }
    return true;
}

/**
 * @brief insertRowData   插入行数据
 * @param rowId 节点id
 * @param rowData 插入的行数据
 * @param index 引索第几个
 * @param nodeParentId 父级Id
 * @param nodeType 节点类型
 * @return 返回插入行数据是否成功
 */
bool GY_ControlTreeView::insertRowData(const QVariant &rowId, const QVector<QVariant> &rowData, const int index, const QVariant &nodeParentId, const QVariant nodeType)
{
    //先父级添加一行数据
    if(!GY_IdTreeView::insertRowData(rowId,rowData,index,nodeParentId,nodeType)){
        return false;
    }
    //循环这一行那些列需要插入控件
    QMap<qint32,QString>::iterator iter;
    for(iter = m_RegisterInfo.begin(); iter!=m_RegisterInfo.end(); ++iter){
        QWidget* pWidget=(QWidget*)m_Reflection.newInstance(iter.value().toLatin1(),Q_ARG(QWidget*,this));
        if(pWidget==nullptr){
            continue;
        }
        //查看对象是否继承自RegisterControlBase类
        if(!pWidget->inherits("RegisterControlBase"))
        {
            pWidget->deleteLater();
            continue;
        }
        //添加控件
        setIndexWidget(m_pFilter->mapFromSource(m_TreeNodeInfo[rowId][iter.key()]->index()),pWidget);
        //让控件显示应有的值
        ((RegisterControlBase*)pWidget)->setData(rowData[iter.key()]);
        ((RegisterControlBase*)pWidget)->setPosition(rowId,iter.key());
        //清空需要插入控件的单元格内容
        m_pModel->blockSignals(true);
        m_TreeNodeInfo[rowId][iter.key()]->setText("");
        m_pModel->blockSignals(false);
        //绑定控件点击事件和表格的点击信号
        connect(((RegisterControlBase*)pWidget), &RegisterControlBase::controlDataUpdate, this, &GY_ControlTreeView::slt_ControlDataUpdate);
    }
    return true;
}

/**
 * @brief slt_ControlDataUpdate 控件数据更新
 * @param rowId 行Id
 * @param columnIndex 列索引
 * @param data 更新后的数据
 */
void GY_ControlTreeView::slt_ControlDataUpdate(const QVariant &rowId, qint32 columnIndex, const QVariant &data)
{
    setCellData(rowId,columnIndex,data,TreeDataType::TransferData);
    emit sgl_CellValueChange(rowId,columnIndex,data);
}
